import _extends from 'babel-runtime/helpers/extends'
import _defineProperty from 'babel-runtime/helpers/defineProperty'
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties'
import PropTypes from 'ant-design-vue/es/_util/vue-types'
import hasProp, {
  getOptionProps,
  getComponentFromProp,
  getListeners
} from 'ant-design-vue/es/_util/props-util'
import ZVcSwitch from './ZVcSwitch'
import Wave from 'ant-design-vue/es/_util/wave'
import Icon from 'ant-design-vue/es/icon'
import { ConfigConsumerProps } from 'ant-design-vue/es/config-provider/configConsumerProps'
import Base from 'ant-design-vue/es/base'
import warning from 'ant-design-vue/es/_util/warning'

const Switch = {
  name: 'ZSwitch',
  __ANT_SWITCH: true,
  model: {
    prop: 'checked',
    event: 'change'
  },
  props: {
    prefixCls: PropTypes.string,
    // size=default and size=large are the same
    size: PropTypes.oneOf(['small', 'default', 'large']),
    disabled: PropTypes.bool,
    checkedChildren: PropTypes.any,
    unCheckedChildren: PropTypes.any,
    tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    checked: PropTypes.string.def('1'),
    defaultChecked: PropTypes.string.def('1'),
    autoFocus: PropTypes.bool,
    loading: PropTypes.bool
  },
  inject: {
    configProvider: {
      default: function _default () {
        return ConfigConsumerProps
      }
    }
  },
  methods: {
    focus: function focus () {
      this.$refs.refSwitchNode.focus()
    },
    blur: function blur () {
      this.$refs.refSwitchNode.blur()
    }
  },
  created: function created () {
    warning(
      hasProp(this, 'checked') || !hasProp(this, 'value'),
      'Switch',
      '`value` is not validate prop, do you mean `checked`?'
    )
  },
  render: function render () {
    let _classes

    const h = arguments[0]

    const _getOptionProps = getOptionProps(this)
      const customizePrefixCls = _getOptionProps.prefixCls
      const size = _getOptionProps.size
      const loading = _getOptionProps.loading
      const disabled = _getOptionProps.disabled
      const restProps = _objectWithoutProperties(_getOptionProps, [
        'prefixCls',
        'size',
        'loading',
        'disabled'
      ])

    const getPrefixCls = this.configProvider.getPrefixCls
    const prefixCls = getPrefixCls('switch', customizePrefixCls)

    const classes =
      ((_classes = {}),
      _defineProperty(_classes, prefixCls + '-small', size === 'small'),
      _defineProperty(_classes, prefixCls + '-loading', loading),
      _classes)
    const loadingIcon = loading
      ? h(Icon, {
          attrs: { type: 'loading' },
          class: prefixCls + '-loading-icon'
        })
      : null
    const switchProps = {
      props: _extends({}, restProps, {
        prefixCls: prefixCls,
        loadingIcon: loadingIcon,
        checkedChildren: getComponentFromProp(this, 'checkedChildren'),
        unCheckedChildren: getComponentFromProp(this, 'unCheckedChildren'),
        disabled: disabled || loading
      }),
      on: getListeners(this),
      class: classes,
      ref: 'refSwitchNode'
    }
    return h(
      Wave,
      {
        attrs: { insertExtraNode: true }
      },
      [h(ZVcSwitch, switchProps)]
    )
  }
}

/* istanbul ignore next */
Switch.install = function (Vue) {
  Vue.use(Base)
  Vue.component(Switch.name, Switch)
}

export default Switch
